import { base_url } from './config.js';

export function getAllPostType() {
  return new Promise((resolve, reject) => {
    uni.request({
      url: base_url + '/post/postType',
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Authorization': uni.getStorageSync('token'),
      },
      success(res) {
        console.log('【 Response in request getAllPostType 】', res);
        resolve(res.data);
      },
      fail(err) {
        console.log('【 Error in request getAllPostType 】', err);
        reject({ success: false });
      }
    });
  });
}

export function getPostListLazyLoad(cursor, limit) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${base_url}/post/post-list?limit=${limit}&${cursor ? `cursor=${cursor}` : ''}`,
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Authorization': uni.getStorageSync('token'),
      },
      success(res) {
        console.log('【 Response in request getPostListLazyLoad 】', res);
        resolve(res.data);
      },
      fail(err) {
        console.log('【 Error in request getPostListLazyLoad 】', err);
        reject({ success: false });
      }
    });
  });
}

export function addPost(postData) {
  return new Promise((resolve, reject) => {
    uni.request({
      url: `${base_url}/post/post`,
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'Authorization': uni.getStorageSync('token'),
      },
      data: postData,
      success(res) {
        console.log('【 Response in request addPost 】', res);
        resolve(res.data);
      },
      fail(err) {
        console.log('【 Error in request addPost 】', err);
        reject({ success: false });
      }
    });
  });
}